<?php
class Lg extends CI_Controller{

        private $limit = 10;
	function  __construct() {
        parent::__construct();
        $this->load->model('Lg_model');
	$this->load->helper(array('form', 'url'));
	$this->load->library(array('form_validation','pagination'));
    }

    function index($offset=''){

       $this->load->library('pagination'); //load library pagination
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);
		$config['base_url'] = site_url('lg/index/');
		$config['total_rows'] = $this->Lg_model->countData();
		$config['per_page'] = $this->limit;
		$config['num_links'] = 2;
		$config['full_tag_open'] = '<div id="pagination">';
		$config['full_tag_close'] = '</div>';

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['offset']=$offset;
		$data['jumlah_data']=$this->Lg_model->countData();
		$data['lg']	= $this->Lg_model->get_list_data($this->limit,$offset);

	//$data['lg'] = $this->Lg_model->getLg();

        $this->load->view('admin_views/lg/lg', $data);

    }
	 function lookup(){
	$keyword = $this->input->post('term');
        $data['response'] = 'false';
        $query = $this->Lg_model->selectDealer($keyword);
        if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {
                $data['message'][] = array(
                                        'id'=>$row->intid_dealer,
                                        'value' => $row->strnama_dealer,
                                        ''
                                     );
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data);
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	function add()
	{
		$this->form_validation->set_rules('intid_tipelg', 'Tipe LG', 'trim|required|xss_clean');
		$this->form_validation->set_rules('intid_dealer', 'Dealer', 'trim|required|xss_clean');
		$this->form_validation->set_rules('intid_omset', 'Omset', 'trim|required|xss_clean');


		$this->form_validation->set_message('required', ' %s tidak boleh kosong !!');

		if ($this->form_validation->run() == FALSE)
		{
			$tlg = $this->Lg_model->selectTlg();
                       foreach ($tlg as $g)

			{
				$data['id'][]	 	= $g->intid_tipelg;
				$data['nama'][] 	= $g->inttotal;
			}
            /*$dealer = $this->Lg_model->selectDealer();
			foreach ($dealer as $g)
			{
				$data['idd'][]          = $g->intid_dealer;
				$data['namad'][] 	= $g->strnama_dealer;
			}*/
                        $omset = $this->Lg_model->selectOmset();
			foreach ($omset as $g)
			{
				$data['idset'][]	= $g->intid_omset;
				$data['namaset'][] 	= $g->intomset;
			}
			$this->load->view('admin_views/lg/add', $data);

		}else {
			$this->Lg_model->insert($_POST);
			redirect('lg/index');
		}
	}
		function edit($id)
		{
			if($_POST==NULL)
			{
				$tlg = $this->Lg_model->selectTlg();
                                foreach ($tlg as $g)
			{
				$data['id'][]	 	= $g->intid_tipelg;
				$data['nama'][] 	= $g->inttotal;
			}
                        $dealer = $this->Lg_model->selectDealer();
			foreach ($dealer as $g)
			{
				$data['idd'][]          = $g->intid_dealer;
				$data['namad'][] 	= $g->strnama_dealer;
			}
                        $omset = $this->Lg_model->selectOmset();
			foreach ($omset as $g)
			{
				$data['idset'][]	= $g->intid_omset;
				$data['namaset'][] 	= $g->intomset;
			}

				$datalg = $this->Lg_model->select($id);
				foreach ($datalg as $g)
				{
					$data['intid_lg'] 		= $g->intid_lg;
					$data['intid_tipelg']		= $g->intid_tipelg;
					$data['intid_dealer']	 	= $g->intid_dealer;
					$data['intid_omset'] 		= $g->intid_omset;

				}
				$this->load->view('admin_views/lg/edit', $data);
			}else {

				$this->Lg_model->update($id);
				redirect('lg/index');
			}

		}

	function delete($id)
	{
		$this->Lg_model->delete($id);
		redirect('lg/index');
	}
     //testpage

  function jqGrid(){
  	$this->load->view('admin_views/lg/lg');
  }

  function loadDataGrid(){
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
		$limit = isset($_POST['rows'])?$_POST['rows']:10; // get how many rows we want to have into the grid
		$sidx = isset($_POST['sidx'])?$_POST['sidx']:'intid_lg'; // get index row - i.e. user click to sort
		$sord = isset($_POST['sord'])?$_POST['sord']:''; // get the direction

        $start = $limit*$page - $limit; // do not put $limit*($page - 1)
		$start = ($start<0)?0:$start;  // make sure that $start is not a negative value

		$where = ""; //if there is no search request sent by jqgrid, $where should be empty
        $searchField = isset($_POST['searchField']) ? $_POST['searchField'] : true;
        $searchOper = isset($_POST['searchOper']) ? $_POST['searchOper']: true;
        $searchString = isset($_POST['searchString']) ? $_POST['searchString'] : true;

        if ($_POST['_search'] == 'true') {
            $ops = array(
            'eq'=>'=', //equal
            'ne'=>'<>',//not equal
            'lt'=>'<', //less than
            'le'=>'<=',//less than or equal
            'gt'=>'>', //greater than
            'ge'=>'>=',//greater than or equal
            'bw'=>'LIKE', //begins with
            'bn'=>'NOT LIKE', //doesn't begin with
            'in'=>'LIKE', //is in
            'ni'=>'NOT LIKE', //is not in
            'ew'=>'LIKE', //ends with
            'en'=>'NOT LIKE', //doesn't end with
            'cn'=>'LIKE', // contains
            'nc'=>'NOT LIKE'  //doesn't contain
            );

            foreach ($ops as $key=>$value){
                if ($searchOper==$key) {
                    $ops = $value;
                }
            }
            if($searchOper == 'eq' ) $searchString = $searchString;
            if($searchOper == 'bw' || $searchOper == 'bn') $searchString .= '%';
            if($searchOper == 'ew' || $searchOper == 'en' ) $searchString = '%'.$searchString;
            if($searchOper == 'cn' || $searchOper == 'nc' || $searchOper == 'in' || $searchOper == 'ni') $searchString = '%'.$searchString.'%';

            $where = "$searchField $ops '$searchString' ";
        }

		if(!$sidx) $sidx =1;
        $count = $this->db->count_all_results('lg');

		if( $count > 0 ) {
			$total_pages = ceil($count/$limit);    //calculating total number of pages
		} else {
			$total_pages = 0;
		}

		if ($page > $total_pages) $page=$total_pages;
   		$query = $this->Lg_model->getAllGrid($start,$limit,$sidx,$sord,$where);

		$responce->page = $page;
		$responce->total = $total_pages;
		$responce->records = $count;
		$i=0;
		foreach($query as $row) {
			$responce->rows[$i]['intid_lg']=$row->intid_lg;
			$responce->rows[$i]['cell']=array($row->inttotal,$row->strnama_dealer,$row->intomset);
			$i++;
		}
		echo json_encode($responce);
  }
//end of test page

}
?>
